package Presetacion;

import java.awt.Graphics;

import javax.swing.ImageIcon;
import javax.swing.JLabel;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Ficha.java
 *
 * Created on 08-oct-2011, 18:28:39
 */
/**
 *
 * @author Rodrigo
 */
public class Ficha extends JLabel {

    private int id;
    private ImageIcon imagen;
    private Casillero casilla;
    private boolean vacio = false;

    /** Creates new form Ficha */
    public Ficha() {
        this(0, "", false);
    }

    public Ficha(int id, ImageIcon img, boolean vacio) {
        initComponents();
        imagen = img;
        this.vacio = vacio;
        this.id = id;       
    }

    public Ficha(int id, String texto, boolean vacio) {
        initComponents();
        this.setText(texto);
        imagen = null;
        this.vacio = vacio;
        this.id = id;
    }

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        if (imagen != null) {
            g.drawImage(imagen.getImage(), 0, 0, getWidth(), getHeight(), this);
        }        
    }

    public boolean isVacio() {
        return vacio;
    }

    public void setVacio(boolean vacio) {
        this.vacio = vacio;
    }

    public Casillero getCasillero() {
        return casilla;
    }

    public int getId() {
        return id;
    }

    public void setCasillero(Casillero cas) {
        if (casilla == cas) {
            return;
        }
        casilla = cas;
        casilla.setFicha(this);

    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        setFont(new java.awt.Font("Calibri", 1, 30)); // NOI18N
        setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        setText("O");
        addMouseListener(new java.awt.event.MouseAdapter() {
            public void mousePressed(java.awt.event.MouseEvent evt) {
                formMousePressed(evt);
            }
        });
    }// </editor-fold>//GEN-END:initComponents

    private void formMousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_formMousePressed
        // TODO add your handling code here:
        if(casilla.getTablero().isEnabled()==false)
            return;
        
        for (Casillero c : casilla.getVecionos()) {
            if (c.getFicha().isVacio()) {
                c.getFicha().setCasillero(casilla);
                casilla.repaint();
                this.setCasillero(c);
                c.repaint();
            }
        }
        casilla.getTablero().validar();

    }//GEN-LAST:event_formMousePressed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    // End of variables declaration//GEN-END:variables
}
